<!doctype html>
<!--
  ~ Copyright (c) 2005, 2014 vacoor
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  -->
<html class="app">
<head>
    <meta charset="utf-8">
    <title>Groovy Shell - WDB</title>
    <link rel="bookmark" href="favicon.ico"/>
    <link rel="shortcut icon" href="img/groovy.ico"/>
    <link rel="stylesheet" href="css/terminal.css"/>
    <script type="text/javascript" src="js/lib/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="js/lib/jquery.mousewheel-min.js"></script>
    <script type="text/javascript" src="js/lib/jquery.terminal-min.js"></script>
</head>
<body style="width: 100%;">
<div id="loader">...</div>
<script type="text/javascript">
    (function ($) {
        function openTerminal(el) {
            $('#loader').hide();
            $(el).terminal(function (command, term) {
                if (!command) {
                    return;
                }
                if ("exit" == command) {
                    command = "request.getSession().removeAttribute('groovy_shell'); System.gc();"
                    $("#loader").show();
                    term.destroy();
                }

                term.pause();
                $.ajax({
                    url: 'gsh/',
                    data: { script: command },
                    dataType: 'json',
                    success: function (data, textStatus, jqXHR) {
                        term.echo(data.result);
                        term.resume();
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        term.error(errorThrown);
                        term.resume();
                    }
                });
            }, {
                greetings: //"Groovy Web Console\nbinding.variables: all variables",
                        " __    __    _ _                 ___                              __ _          _ _ \r\n" +
                        "/ / /\\ \\ \\__| | |__             / _ \\_ __ ___   _____   ___   _  / _\\ |__   ___| | |\r\n" +
                        "\\ \\/  \\/ / _` | '_ \\   _____   / /_\\/ '__/ _ \\ / _ \\ \\ / / | | | \\ \\| '_ \\ / _ \\ | |\r\n" +
                        " \\  /\\  / (_| | |_) | |_____| / /_\\\\| | | (_) | (_) \\ V /| |_| | _\\ \\ | | |  __/ | |\r\n" +
                        "  \\/  \\/ \\__,_|_.__/          \\____/|_|  \\___/ \\___/ \\_/  \\__, | \\__/_| |_|\\___|_|_|\r\n" +
                        "                                                          |___/",
                prompt: "groovy> ",
                exit: false
            });
        }

        $('#loader').bind('click', function () {
            openTerminal(document.body);
        });

        /autoConnect=([^?&]+)/.exec(location.search)[1] && openTerminal(document.body);
    })(jQuery);
</script>
</body>
</html>
